<?php
namespace Hilaoyu\ComputeCloudClient;

use Hilaoyu\ComputeCloudClient\Api\CtyunEcs;
use Hilaoyu\ComputeCloudClient\Api\CtyunVpc;
use Hilaoyu\ComputeCloudClient\Api\HuaWeiEcs;
use Hilaoyu\ComputeCloudClient\Api\HuaWeiVpc;
use Hilaoyu\ComputeCloudClient\ctyun\CtyunCLient;
use Hilaoyu\ComputeCloudClient\ctyun\CtyunClientEcs;
use Hilaoyu\ComputeCloudClient\ctyun\CtyunClientIms;
use Hilaoyu\ComputeCloudClient\ctyun\CtyunClientVpc;
use Hilaoyu\ComputeCloudClient\Interfaces\ComputeCloudClientInterface;
use Hilaoyu\Utils\Traits\ErrorsTrait;
use IPTools\Network;


class CtyunApi implements ComputeCloudClientInterface
{
    use ErrorsTrait;

    protected $accessKeyId;
    protected $accessKeySecret;
    protected $projectId;

    protected $ctyunClient;

    static public function make($accessKeyId, $accessKeySecret,$projectId=null){
        return new static($accessKeyId, $accessKeySecret,$projectId);
    }

    public function __construct($accessKeyId, $accessKeySecret,$projectId=null){
        $this->accessKeyId = $accessKeyId;
        $this->accessKeySecret = $accessKeySecret;
        $this->projectId = $projectId;
    }



    /**
     * @param string $regionId
     * @return CtyunVpc
     */
    public function vpc($regionId=''){

        return new CtyunVpc(new CtyunClientVpc($this->accessKeyId,$this->accessKeySecret,$this->projectId,$regionId),$this,$regionId);

    }
    public function cbn($regionId=''){

        return new CtyunVpc(new CtyunClientVpc($this->accessKeyId,$this->accessKeySecret,$this->projectId,$regionId),$this,$regionId);

    }

    /**
     * @param $regionId
     * @return CtyunEcs
     */
    public function ecs($regionId=''){
        return new CtyunEcs(new CtyunClientEcs($this->accessKeyId,$this->accessKeySecret,$this->projectId,$regionId),$this,$regionId);

    }    /**
     * @param $regionId
     * @return CtyunEcs
     */
    public function ims($regionId=''){
        return new CtyunEcs(new CtyunClientIms($this->accessKeyId,$this->accessKeySecret,$this->projectId,$regionId),$this,$regionId);

    }



    static public function GetGateWay(string $network) :string{
        try{
            $network = Network::parse($network);
            //return (string)$network->getLastIP()->prev();
            return (string)$network->getFirstIP()->next();
        }catch (\Exception $exception){
            return "";
        }
    }
}
